<?php
/**
 *  玩家一键删除已读没附件的邮件与已读已领取附件的邮件
 */

class OneKeyDel extends APIBase {
	public $tag = "OneKeyDel";
	public $isLogin = true;
	
	public function before() {
		$this->initMysql();
		$this->inithttp();
		$this->initCacheRedis();
		return true;
	}
			
	public function logic() {
	    $uid = $this->uid;
		$keys = $this->cache_redis->keys('hemailbyuser:'.$uid.':*');
		$data = array();
		if(count($keys)>0){
			$msg = array();
			$msg0 = array();
			for($i=1;$i<=count($keys);$i++){
				//获取玩家的邮件
				$emailinfo = $this->cache_redis->hMget('hemailbyuser:'.$uid.':'.$i,array('mid','is_receive','is_read'));
				//判断出没领取的邮件
				if($emailinfo['is_read']!=9){
				    if($emailinfo['is_read']==1){
				        $hmg = $this->cache_redis->hMget('hmg:'.$emailinfo['mid'],array('rid'));
				        //判断邮件是否有附件可领取
				        if($hmg['rid']!=''){
					        if($emailinfo['is_receive']==1){
							    //改变邮件状态
						        $this->cache_redis->hMset('hemailbyuser:'.$uid.':'.$i,array('is_read'=>9));
						    }else{
							    $msg[] = $i; 
						    }
				        }else{
						    //改变邮件状态
						    $this->cache_redis->hMset('hemailbyuser:'.$uid.':'.$i,array('is_read'=>9));
					    }
				    }else{
					    $msg0[] = $i;
				    }
				}
			}
			if(!empty($msg)){
				$this->returnError(302,'有部分邮件附件未领取');
			}elseif(!empty($msg0)){
				$this->returnError(303,'有部分邮件附件未读取');
			}else{
				$this->returnData('success');
			}
		}elseif(count($keys)<=0){
			$this->returnError(301,'玩家尚未有邮件可删除');
		}
	}

	public function after() {
		$this->deinitDataRedis();
		$this->deinitMysql();
	}
}

?>
